package com.yanqu.road.tools;


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;

public class ConfigImport {
    public static void main(String[] args) {
        String dirPath = System.getProperty("user.dir") + File.separator;
//        String dirPath = "D:\\importconfig\\";
//        String excelPath = "D:\\game-config\\港澳台" + File.separator;
        String excelPath = dirPath + "excelDir" + File.separator;
        String xmlPath = dirPath + "excelDir" + File.separator;
        String configPath = dirPath + "config" + File.separator;

        System.out.println("configPath:" + configPath);
        // 获取数据库连接
        Connection conn = initConn(configPath);
        if (conn == null) {
            throw new  RuntimeException("数据库连接失败");
        }
        // 初始化映射表
        initExcelTableMapper(xmlPath+"MappingConfig.xml");

        Map<String, String> fileMapperMap = ExcelTableMapper.getFileMapperMap();
        int index = 0;
        int totalTable = fileMapperMap.size();
        for (Map.Entry<String, String> entry : fileMapperMap.entrySet()) {
            String excelName = entry.getKey();
            String tableName = entry.getValue();
            ExcelData excelData = new ExcelReader(excelPath + excelName).read();
            System.out.println("==========start import " + (index +1) + "/" + totalTable + "============");
            new TableImport(conn, tableName, excelData).importData();
            index++;
        }

        try {
            conn.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    static void initExcelTableMapper(String mapperFile) {
        ExcelTableMapper.initMapper(mapperFile);
    }

    static Connection initConn(String configPath) {
        File pathFile = new File(configPath);
        for (File file : pathFile.listFiles()) {
            if (file.getName().startsWith("db") && file.getName().endsWith("properties")) {
                Properties properties = loadProp(file);
                String connConfigStr = properties.getProperty("db_road_config.url");
               return parseDbConfig(connConfigStr);
            }

        }
        return null;
    }



    public static Connection parseDbConfig(String connConfigStr) {
        String[] arr = connConfigStr.split("\\|");
        String url = arr[0];
        String userName = arr[1];
        String password = arr[2];
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.err.println("load jdbc driver fail!!");
            throw new RuntimeException(e);
        }
        try {
            Connection conn = DriverManager.getConnection(url, userName, password);
            conn.setAutoCommit(false);
            return conn;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static Properties loadProp(File file) {
        Properties prop = new Properties();
        InputStream inputStream = null;
        try {
            inputStream = Files.newInputStream(file.toPath());
            prop.load(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                assert inputStream != null;
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return prop;
    }
}
